/**
 * 
 */
package se.izenman.vlc;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

/**
 * @author lusta
 * 
 */
public class ControlVLCRobot implements ITCPVLCFunctions {

	private Robot robot;
	private String osName;

	/**
	 * Constructeur de la class permettant le controle de vlc
	 */
	public ControlVLCRobot() {
		try {
			robot = new Robot();
			osName = System.getProperty("os.name");
		} catch (AWTException e) {
		}
	}

	/**
	 * Débute la lecture sur vlc
	 */
	@Override
	public int play() {

		if (osName.equals("Mac OS X")){
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_P);
			this.robot.keyRelease(KeyEvent.VK_P);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_SPACE);
			this.robot.keyRelease(KeyEvent.VK_SPACE);
		}
		return 0;
	}

	/**
	 * Met la lecture en pause
	 */
	@Override
	public int pause() {
		this.play();
		return 0;
	}

	/**
	 * Stop la lecture sur vlc
	 */
	@Override
	public int stop() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_PERIOD);
			this.robot.keyRelease(KeyEvent.VK_PERIOD);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_S);
			this.robot.keyRelease(KeyEvent.VK_S);
		}
		return 0;
	}

	/**
	 * Passe à l'élément suivant dans la liste de lecture sur vlc
	 */
	@Override
	public int next() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_RIGHT);
			this.robot.keyRelease(KeyEvent.VK_RIGHT);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_N);
			this.robot.keyRelease(KeyEvent.VK_N);
		}
		return 0;
	}

	/**
	 * Passe à l'élément précedent dans la liste de lecture de vlc
	 */
	@Override
	public int previous() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_LEFT);
			this.robot.keyRelease(KeyEvent.VK_LEFT);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_P);
			this.robot.keyRelease(KeyEvent.VK_P);
		}
		return 0;
	}

	/**
	 * Ralentit la vitesse de lecture sur vlc
	 */
	@Override
	public int ralentit() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_ALT);
			this.robot.keyPress(KeyEvent.VK_LEFT);
			this.robot.keyRelease(KeyEvent.VK_LEFT);
			this.robot.keyRelease(KeyEvent.VK_ALT);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_MINUS);
			this.robot.keyRelease(KeyEvent.VK_MINUS);
		}
		return 0;
	}

	/**
	 * Active l'avance rapide sur vlc
	 */
	@Override
	public int avanceRapide() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_ALT);
			this.robot.keyPress(KeyEvent.VK_RIGHT);
			this.robot.keyRelease(KeyEvent.VK_RIGHT);
			this.robot.keyRelease(KeyEvent.VK_ALT);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_PLUS);
			this.robot.keyRelease(KeyEvent.VK_PLUS);
		}
		return 0;
	}

	/**
	 * Coupe le volume sur vlc
	 */
	@Override
	public int mute() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_ALT);
			this.robot.keyPress(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_ALT);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_M);
			this.robot.keyRelease(KeyEvent.VK_M);
		}
		return 0;
	}

	/**
	 * Diminue le volume sur vlc
	 */
	@Override
	public int volumeUp() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_UP);
			this.robot.keyRelease(KeyEvent.VK_UP);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_CONTROL);
			this.robot.keyPress(KeyEvent.VK_UP);
			this.robot.keyRelease(KeyEvent.VK_UP);
			this.robot.keyRelease(KeyEvent.VK_CONTROL);
		}
		return 0;
	}

	/**
	 * Augmente le volume sur vlc
	 */
	@Override
	public int volumeDown() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_CONTROL);
			this.robot.keyPress(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_DOWN);
			this.robot.keyRelease(KeyEvent.VK_CONTROL);
		}
		return 0;
	}

	/**
	 * Active la répétition sur vlc
	 */
	@Override
	public int repeat() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_L);
			this.robot.keyRelease(KeyEvent.VK_L);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_L);
			this.robot.keyRelease(KeyEvent.VK_L);
		}
		return 0;
	}

	/**
	 * Active le random sur vlc
	 */
	@Override
	public int random() {
		if (osName.equals("Mac OS X")) {
			this.robot.keyPress(KeyEvent.VK_META);
			this.robot.keyPress(KeyEvent.VK_W);
			this.robot.keyRelease(KeyEvent.VK_W);
			this.robot.keyRelease(KeyEvent.VK_META);
		} else {
			this.robot.keyPress(KeyEvent.VK_R);
			this.robot.keyRelease(KeyEvent.VK_R);
		}
		return 0;
	}

}
